#include "stdafx.h"
#include "2DUtils.h"

#include "InputLayouts.h"
#include "LoadShader.h"
#include "Renderer.h"

#include "GlobalAppData.h"

using namespace mgfx;

static VertexBufferID quadVertexBuffer;
static IndexBufferID quadIndexBuffer;

static ShaderID singleColorEffect;
static ShaderID texturedEffect;

static ID3D11Device * device;
static ID3D11DeviceContext * context;

static void createQuad()
{
	VertexPosition vertices[4];

	vertices[0].Position = XMFLOAT3( 0.0f, -1.0f, 0.5f);
	vertices[1].Position = XMFLOAT3( 0.0f,  0.0f, 0.5f);
	vertices[2].Position = XMFLOAT3( 1.0f, -1.0f, 0.5f);
	vertices[3].Position = XMFLOAT3( 1.0f,  0.0f, 0.5f);

	quadVertexBuffer = Renderer::Instance->addVertexBuffer(&vertices[0], 4, sizeof(VertexPosition));
	
	// index buffer
	short indices[6];

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;

	indices[3] = 1;
	indices[4] = 3;
	indices[5] = 2;

	quadIndexBuffer = Renderer::Instance->addIndexBuffer(indices, 6, sizeof(short));
}

void mgfx::Utils2D::init(ID3D11Device * dev, ID3D11DeviceContext * devCon)
{
	createQuad();
	singleColorEffect = loadShaderFromFile("SingleColor2D.fx");
	texturedEffect = loadShaderFromFile("Textured2D.fx");
}

void mgfx::Utils2D::deinit()
{
}

void mgfx::Utils2D::drawRect(uint x, uint y, uint width, uint height, XMVECTOR color)
{
	uint scrWid = mcore::GlobalAppData::Instance.ScreenWidth;
	uint scrHei = mcore::GlobalAppData::Instance.ScreenHeight;

	uint stride = sizeof(mgfx::VertexPosition);
	uint offset = 0;

	XMMATRIX transform = XMMatrixMultiply(
		XMMatrixScaling(width / float(scrWid / 2), height / float(scrHei / 2), 1.0f),
		XMMatrixTranslation(x / float(scrWid / 2) - 1.0f, -int(y) / float(scrHei / 2) + 1.0f, 0.0f)
	);

	Renderer * rdr = Renderer::Instance;
	rdr->setVertexBuffer(quadVertexBuffer);
	rdr->setIndexBuffer(quadIndexBuffer);
	rdr->setPrimitiveType(TRIANGLE_LIST);
	rdr->setVertexFormat(mgfx::VertexPosition::VertexFormat);
	rdr->setShader(singleColorEffect);
	rdr->setShaderMatrix("gWorld", (float*)&transform);
	rdr->setShaderFloatVector("gColor", (float*)&color);
	rdr->drawIndexed(6);
}

void mgfx::Utils2D::drawRectTextured(uint x, uint y, uint width, uint height, TextureID tex)
{
	uint scrWid = mcore::GlobalAppData::Instance.ScreenWidth;
	uint scrHei = mcore::GlobalAppData::Instance.ScreenHeight;

	uint stride = sizeof(mgfx::VertexPosition);
	uint offset = 0;

	XMMATRIX transform = XMMatrixMultiply(
		XMMatrixScaling(width / float(scrWid / 2), height / float(scrHei / 2), 1.0f),
		XMMatrixTranslation(x / float(scrWid / 2) - 1.0f, -int(y) / float(scrHei / 2) + 1.0f, 0.0f)
	);

	Renderer * rdr = Renderer::Instance;
	rdr->setVertexBuffer(quadVertexBuffer);
	rdr->setIndexBuffer(quadIndexBuffer);
	rdr->setPrimitiveType(TRIANGLE_LIST);
	rdr->setVertexFormat(mgfx::VertexPosition::VertexFormat);
	rdr->setShader(texturedEffect);
	rdr->setShaderMatrix("gWorld", (float*)&transform);
	rdr->setShaderResource("gTexture", tex);
	rdr->drawIndexed(6);
}